/*
 * @(#)CustomerAction.java 创建于 2014年3月14日 
 * 
 * Copyright (c) 2013-2014 by Drow.  
 *
 */
package com.hangxin.basic.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hangxin.basic.model.Company;
import com.hangxin.basic.model.Customer;
import com.hangxin.basic.model.LoginInfo;
import com.hangxin.basic.service.CompanyService;
import com.hangxin.basic.service.CustomerService;
import com.hangxin.basic.service.DictInfoService;
import com.hangxin.basic.service.UserService;
import com.hangxin.basic.util.Content;
import com.hangxin.commons.util.MD5Util;
import com.hangxin.commons.util.PageModel;
import com.hangxin.commons.web.BasicAction;
import com.hangxin.system.model.Syslog;
import com.hangxin.system.service.SyslogService;

@Controller
public class CustomerAction extends BasicAction
{
	static final Logger logger = LoggerFactory.getLogger(CompanyAction.class);
	@Resource
	private CustomerService customerService;// 添加登录用户service接口实现

	@Resource
	private SyslogService syslogServiceImpl;// 系统日志操作的Service

	/**
	 * 
	 * 功能描述：客户信息管理
	 * 
	 * 客户管理
	 * <P/>
	 * 创建时间：2014年8月6号
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param request
	 * @param pageno
	 * @param pages
	 * @param orgid
	 * @param model
	 * @return
	 */
	@RequestMapping(value="customerManageAction")
	public String customerManageAction(HttpServletRequest request, Integer pageno, Integer pages, Integer pageSize, String ccode,String cname,String cnum,String cadtel,String caccount, Model model)
	{
		PageModel p=new PageModel();
		Boolean bool = false;
		pages = pages == null ? 1 : pages;
		pageno = pageno == null ? 1 : pageno;
		pageSize = pageSize == null ? 12 : pageSize;
		p.setPages(pages);// 必须先比pageno设置
		p.setPageNo(pageno);
		p.setPageSize(pageSize);
		ccode=(ccode==null)?"":ccode;
		cname=(cname==null)?"":cname;
		cnum=(cnum==null)?"":cnum;
		cadtel=(cadtel==null)?null:cadtel;
		caccount=(caccount==null)?null:caccount;
		
		p.setPages(pages);p.setPageNo(pageno);p.setPageSize(pageSize);
		PageModel<Map<String, Object>> pageModel = customerService.customerManage(p,  ccode, cname, cnum, cadtel, caccount);
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("ccode", ccode);
		model.addAttribute("cname", cname);
		model.addAttribute("cnum", cnum);
		model.addAttribute("cadtel", cadtel);
		model.addAttribute("caccount", caccount);
		return "system/customer/customerManage";
	}

	/**
	 * 添加客户初始化
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="customerAddAction0")
	public String customerAddAction0(HttpServletRequest request, Model model){
		return "system/customer/customerAdd";
	}
	
	@RequestMapping(value = "/CustomerActionImport")
	public String LicenseActionImport(HttpServletRequest request, Model model)
	{   
		return "system/customer/custImport";
	}
	/**
	 * 
	 * 功能描述：添加客户信息
	 * <P/>
	 * 创建时间：2014年8月6号
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param request
	 * @param username
	 * @param employeeid
	 * @param companyid
	 * @param model
	 * @return
	 */
	@RequestMapping(value="customerAddAction1")
	public void customerAddAction1(HttpServletResponse response, HttpServletRequest request,Customer customer, Model model, HttpSession session)
	{	
		List<Integer> list=new ArrayList<Integer>();
		if(request.getSession().getAttribute("realname")==null){
			list.add(0);
		}else if(request.getSession().getAttribute("realname").equals("")){
			list.add(0);
		}else{
//			if(customerService.checkName("code", customer.getCcode(),null)>0){
//				list.add(1);
//			}else{
				if(customerService.checkName("name", customer.getCname(),null)>0){
					list.add(2);
				}else{
//					if(customerService.checkName("taxno", customer.getCnum(),null)>0){
//						list.add(3);
//					}else{
						Long applicant = (Long) request.getSession().getAttribute("lgid");
						Integer p = customerService.customerAdd(customer);
						//添加系统操作日志
						StringBuffer content = new StringBuffer();
						content.append("用户：");
						content.append(request.getSession().getAttribute("realname"));
						content.append("，于");
						content.append(new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format((new Date().getTime())));
						content.append("添加客户");
						Syslog syslog = new Syslog(Long.valueOf(request.getSession().getAttribute("lgid").toString()), request.getSession().getAttribute("realname").toString(), request.getSession().getAttribute("companyname").toString(), "添加", "用户添加", content.toString(), Long.parseLong(request.getSession().getAttribute("lgid").toString()));
						syslog.setAddtime(new Timestamp(new Date().getTime()));
						syslogServiceImpl.save(syslog);// 添加操作日志
						list.add(4);
//					}
//				}
			}
		}
		response.setCharacterEncoding("utf-8");
		JSONArray array = new JSONArray();
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		array = JSONArray.fromObject(list);
		out.print(array);
		
	}



	
	/**
	 * 
	 * 功能描述：添加登录用户时验证用户名是否已经存在
	 * <P/>
	 * 创建时间：2014年8月6号
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping(value="customerCheckAction")
	public Object customerCheckAction(HttpServletResponse response, String cname,String content)
	{
		PrintWriter printWriter = null;
		try
		{
			printWriter = response.getWriter();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (customerService.checkName(cname,content,null) > 0)
		{
			printWriter.print("false");
		} else
		{
			printWriter.print("true");
		}
		return null;
	}



	


	/**
	 * 
	 * 功能描述：根据id查找登录用户
	 * <P/>
	 * 创建时间：2014年8月6日
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param request
	 * @param state
	 * @param employee_id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="customerFindAction")
	public String customerFindAction(HttpServletResponse response, HttpServletRequest request, Long id, Integer pageno, Integer pages, Model model)
	{

		Map<String, Object> map = customerService.customerFind(id);
		model.addAttribute("map", map);
		return "system/customer/customerEdit";
	}



	/**
	 * 
	 * 功能描述：根据id修改登录用户
	 * <P/>
	 * 创建时间：2014年3月20日
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param request
	 * @param state
	 * @param employee_id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="customerEditAction")
	public void customerEditAction(HttpServletResponse response, HttpServletRequest request, Customer customer, Integer pageno, Integer pages, Model model)
	{
		List<Integer> list=new ArrayList<Integer>();
		if(request.getSession().getAttribute("realname")==null){
			list.add(0);
		}else if(request.getSession().getAttribute("realname").equals("")){
			list.add(0);
		}else{
//			if(customerService.checkName("code", customer.getCcode(),customer.getId())>0){
//				list.add(1);
//			}else{
//				if(customerService.checkName("name", customer.getCname(),customer.getId())>0){
//					list.add(2);
//				}else{
//					if(customerService.checkName("taxno", customer.getCnum(),customer.getId())>0){
//						list.add(3);
//					}else{
						customerService.customerEdit(customer);
						list.add(4);
						//添加系统操作日志
						StringBuffer content = new StringBuffer();
						content.append("用户：");
						content.append(request.getSession().getAttribute("realname"));
						content.append("，于");
						content.append(new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format((new Date().getTime())));
						content.append("修改客户信息");
						Syslog syslog = new Syslog(Long.valueOf(request.getSession().getAttribute("lgid").toString()), request.getSession().getAttribute("realname").toString(), request.getSession().getAttribute("companyname").toString(), "修改", "用户修改", content.toString(), Long.parseLong(request.getSession().getAttribute("lgid").toString()));
						syslog.setAddtime(new Timestamp(new Date().getTime()));
						syslogServiceImpl.save(syslog);// 添加操作日志
//					}
//				}
//			}
		}
		response.setCharacterEncoding("utf-8");
		JSONArray array = new JSONArray();
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		array = JSONArray.fromObject(list);
		out.print(array);
		
	}
	
	
	/**
	 * 
	 * 功能描述：删除客户
	 * <P/>
	 * 创建时间：2014年3月19日
	 * <P/>
	 * 创建人： 段志杨
	 * <P/>
	 * 联系方式： 315874529@qq.com
	 * 
	 * @param request
	 * @param state
	 * @param employee_id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="customerDelAction")
	public void customerDelAction(HttpServletResponse response, HttpServletRequest request, String[] id, Model model)
	{
		List<String> list = new ArrayList<String>();
		if(request.getSession().getAttribute("realname")==null){
			list.add("0");
		}else if(request.getSession().getAttribute("realname").equals("")){
			list.add("0");
		}else{
			for (int i = 0; i < id.length; i++) {
				if(customerService.customerDel( Long.parseLong(id[i]))>0){
					list.add("1");
				}else{
					list.add("0");
				}
			}
		}
		response.setCharacterEncoding("utf-8");
		JSONArray array = new JSONArray();
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		array = JSONArray.fromObject(list);
		out.print(array);
		
	}
	/**
	 * 客户导入保存
	 * @param response
	 * @param serflmes
	 * @param model
	 * @return
	 */
	@RequestMapping(value="custImportSave")
	public String custImportSave(HttpServletResponse response, String serflmes, Model model)
	{
		List<String> list = new ArrayList<String>();
		JSONArray array = new JSONArray();
		PrintWriter out = null;
		try {
		Integer n = customerService.custImportSave(serflmes);
		if (n > 0) {
			list.add("1");
		} else {
			list.add("2");
		}
		response.setCharacterEncoding("utf-8");
		out = response.getWriter();
		} catch (Exception e) {
			e.printStackTrace();
		}
		array = JSONArray.fromObject(list);
		out.print(array);
		return null;
	}
}
